package com.lzx.iteam.net;

import android.os.Message;
import android.util.Log;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.StringUtil;
import org.json.JSONException;
import org.json.JSONObject;

import static io.rong.imageloader.core.ImageLoader.TAG;

/**
 * Created by xuepengju on 14-3-20.
 */
public class GetStatusMsg extends CcMsgStructure {

    private Message mCallback;

    public GetStatusMsg(Message callback){
        mCallback = callback;
    }

    @Override
    public void onReceive(JSONObject data) {
        String status="";
        try {
            JSONObject jsonObject = data.getJSONObject(AsynHttpClient.KEY_CC_DATA);
            status=jsonObject.getString("status");
        } catch (JSONException e) {
            Log.d(TAG,e.toString());
            e.printStackTrace();
        }


        if(data != null){
            if(mCallback != null){
                mCallback.arg1 = 0;
                mCallback.obj = status;
                mCallback.sendToTarget();
            }
        }else{
            if(mCallback != null){
                mCallback.arg1 = 2;
                mCallback.sendToTarget();
            }
        }
    }

    @Override
    public void onError(int error, Object ret) {
        String errorMsg = null;
        if (ret != null) {
            JSONObject jData = null;
            try {
                jData = new JSONObject(ret.toString());
                errorMsg = jData.getString("msg");
            } catch (JSONException e) {
                errorMsg = "服务端数据解析失败!";
                e.printStackTrace();
            }

        }
        if(mCallback != null){
            mCallback.arg1 = error;
            if(!StringUtil.isEmpty(errorMsg)){
                mCallback.obj = errorMsg;
            }else{
                mCallback.obj = Constants.NETWORK_WRROR;
            }
            mCallback.sendToTarget();
        }
    }
}
